<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>商品列表</title>
    <link rel="stylesheet" href="https://www.layuicdn.com/layui-v2.5.6/css/layui.css" media="all">
    <style>
        .productList-container {
            padding: 20px;
        }

        td[data-field="imgUrl"] div,
        td[data-field="negativeImgUrl"] div {
            height: 120px;
        }

        .lay-form {
            padding: 20px;
        }
        .productList-container {
            background-color: #fff;
        }
        .bar {
            display: flex;
            justify-content: flex-start;
            align-items: flex-end;
        }
        .searchInput {
            display: flex;
            align-items: flex-end;
        }
        .searchInput input {
            width: 200px;
        }
        .searchInput .layui-form-label {
            padding: 9px 0;
        }
        .bar .layui-form-label {
            padding: 0;
            padding-bottom: 4px;
            text-align: left;
        }
        .bar .layui-inline {
            margin-right: 20px;
        }
        .layui-table img {
            max-width: 200px;
            object-fit: contain;
        }
        .layui-table-cell {
            height: auto;
        }
    </style>
</head>

<body>
    <div class="productList-container">

        <!-- 搜索 -->
        <div class="searchInput">
            <form class="layui-form" action="" lay-filter="resets">
                <div class="layui-inline">
                    <label class="layui-form-label">商品名称：</label>
                    <input class="layui-input" name="goodsName" placeholder="请输入商品名称" autocomplete="off">
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">商品编号：</label>
                    <input class="layui-input" name="spuNo" placeholder="请输入商品编号" autocomplete="off">
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">商品分类：</label>
                    <div class="layui-input-inline">
                        <select name="categoryId" lay-search="" id="productClass">
                          <option value="">请选择商品分类</option>
                          
                        </select>
                      </div>
                </div>  
                <div class="layui-inline">
                    <label class="layui-form-label">创建日期：</label>
                    <input type="text" class="layui-input" id="test1" name="createTimeStart" placeholder="请输入创建日期" autocomplete="off">
                </div>
                <button class="layui-btn" data-type="reload" lay-submit="" lay-filter="searchForm">搜索</button>
                <button class="layui-btn layui-btn-primary" data-type="reload" lay-submit="" lay-filter="reset">重置</button>
            </form>
        </div>
    

        <table lay-filter="productListTable" class="layui-hide" id="productListTable"></table>
    </div>

</body>

<!-- 添加 -->
<script type="text/html" id="toolbarDemo">
    <div class="bar">
        <div class="layui-btn-container" style="margin-bottom:-5px;">
            <button class="layui-btn layui-btn-normal layui-btn-sm" lay-event="add">添加</button>
            <!-- <button class="layui-btn layui-btn-sm" lay-event="success">上架</button>
            <button class="layui-btn layui-btn-danger layui-btn-sm" lay-event="fail">下架</button> -->
        </div>

        <!-- 筛选 -->
        <!-- <div class="layui-form-item" id="status" style="margin-bottom:0">
            <label class="layui-form-label" style="width: 60px; text-align:right; margin-top:10px">推荐状态：</label>
            <div class="layui-input-block" style="margin-left: 70px">
              <select name="stop" lay-filter="select">
                <option value="">全部</option>
                <option value="0">推荐</option>
                <option value="1">未推荐</option>
              </select>
            </div>
        </div> -->
    </div>
</script>

<!-- 操作 -->
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
</script>

<script src="https://www.layuicdn.com/layui-v2.5.6/layui.js"></script>
<script src="../utils.js"></script>
<script>
    layui.use(['table', 'upload', 'form', 'jquery', "laydate"], function () {
        var table = layui.table,
            upload = layui.upload,
            form = layui.form,
            laydate = layui.laydate,
            $ = layui.jquery;

        table.render({
            elem: '#productListTable'
            , toolbar: "#toolbarDemo"
            , url: window.baseUrl + '/goods/getGoodsList'
            , method: 'post'
            , page: false
            , contentType: 'application/json'
            , headers: {
                Authorization: localStorage.token
            }
            , response: {
                statusCode: 200 //规定成功的状态码，默认：0
            }
            , cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            ,parseData: function(res){ //res 即为原始返回的数据
                return {
                    "code": 200,
                    "count": res.data.recordsTotal, //解析数据长度
                    "data": res.data.data //解析数据列表
                };
            }
            ,request: {
                pageName: 'start' //页码的参数名称，默认：page
                ,limitName: 'length' //每页数据量的参数名，默认：limit
            }
            , cols: [[
                {type: 'checkbox'}
                , { field: 'id', width: 80, title: 'ID', sort: true }
                , { field: 'fullName', title: '商品图片', templet: function (res) {
                        let a = res.skuImgUrl.split(",");
                        return "<img style='width:100px; height:100px; object-fit: contain;' class='viewImg' src='"+ a[0] +"'/>"
                }}
                , { field: 'goodsName', title: '商品名称'}
                , { field: 'categoryName', title: '商品分类'}
                // , { field: "recommend", title: '价格', width: 140 }
                , {title: '推荐', width: 100, templet: function (res) {
                    return '<input type="checkbox" '+ (res.recommend == 1 ? 'checked=""' : "") +'" lay-filter="switchTest" lay-skin="switch" lay-text="推荐|未推荐" value="'+ res.recommend +'" data-id="'+ res.id +'"/>'

                }}
                , {title: '上下架状态', width: 100, templet: function (res) {
                        return  "<span>" + (res.status && res.status ==1 ? "上架" : "下架") + "</span>"
                }}
                , { field: "skuSaleNum", title: '销量', width: 80}
                , { field:"skuNum", title: '库存', width: 80}
                , { title: '商品类型', width: 120,  templet: function (res) {
                    return  "<span>" + (!res.special ? "普通商品" : "特殊商品") + "</span>"
                }}
                , { field:"createTime", title: '添加时间', width: 160}
                , { title: '操作', toolbar: '#barDemo', width: 80 }
            ]],
            page: true
            , done: function() {
                // 放大图片
                $(".viewImg").on("click", function() {
                    let url = $(this).attr("src");
                    layer.open({
                        type: 1,
                        title: false,
                        closeBtn: 0,
                        area: ['auto'],
                        skin: 'layui-layer-nobg', //没有背景色
                        shadeClose: true,
                        content: "<img src=" + url + " />"
                    });
                })
            }
        });

        laydate.render({
            elem: '#test1'
            ,range: true
        });

        // 获取分类

        ajax({
            path: "/goods/getCategoryList?length=99999999",
            type: "GET",
            success: function (res) {
                console.log(res.data);
                res.data.forEach(el => {
                    $('#productClass').append(`<option value="${el.id}">${el.categoryName}</option>`)
                })

                form.render();
            }
        }, $)
 

        // 是否推荐
        form.on("switch(switchTest)", function(e) {
            let id = $(e.elem).attr('data-id');
            // 删除
            if (e.value == 1) {
                ajax({
                    path: "/goods/saveOrUpdateGoods",
                    data: {
                        id: id,
                        interfaceType: 1,
                        recommend: 0
                    },
                    success: function (res) {
                        table.reload("productListTable");
                    }
                }, $)
            }
            // 添加 
            else {
                ajax({
                    path: "/goods/saveOrUpdateGoods",
                    data: {
                        id: id,
                        interfaceType: 1,
                        recommend: 1
                    },
                    success: function (res) {
                        table.reload("productListTable");
                    }
                }, $)
            }
        })


        //工具栏事件
        table.on('toolbar(productListTable)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                // case 'success':
                //     //页面层
                //     var checkStatus = table.checkStatus('productListTable')
                //         ,data = checkStatus.data;
                //         if (!data.length) {
                //             return layer.msg("请选择！")
                //         }
                //         data.forEach(el => {
                //             el.status = 1;
                //             ajax({
                //                 path: "/indexUser/editAuthenticate",
                //                 data: el, 
                //                 success: function (res) {
                //                     table.reload("productListTable");
                //                 }
                //             }, $)
                //         })

                //         layer.msg("已通过！")
                //     break;
                // case 'fail':
                //     var checkStatus = table.checkStatus('productListTable')
                //         ,data = checkStatus.data;
                //         if (!data.length) {
                //             return layer.msg("请选择！")
                //         }
                //         data.forEach(el => {
                //             el.status = 2;
                //             ajax({
                //                 path: "/indexUser/editAuthenticate",
                //                 data: el, 
                //                 success: function (res) {
                //                     table.reload("productListTable");
                //                 }
                //             }, $)
                //         })
                        
                //         layer.msg("已驳回！")
                //     break;
                case "add":
                    window.location.href = "../addProduct/addProduct.html";
                    break;
            };
        });

        // 搜索
        form.on("submit(searchForm)", function(e){
            
            for (let key in e.field) {
                if (!e.field[key]) {
                    delete e.field[key]
                }
            }

            if (e.field.createTimeStart) {
                var a = e.field.createTimeStart.split(" - ")
                e.field.createTimeStart = new Date(a[0]).getTime();
                e.field.createTimeEnd = new Date(a[1]).getTime();
            }

            table.reload('productListTable', {
                page: {
                    start: 1 //重新从第 1 页开始
                }
                ,where: e.field
            });

            return false;
        })


        // 搜索
        form.on("submit(reset)", function(e){
            
            table.reload('productListTable', {
                page: {
                    start: 1 //重新从第 1 页开始
                }
                ,where: null
            });

            form.val("resets", {
                createTimeStart: "",
                goodsName: "",
                spuNo: "",
                categoryId: ""
            });

            return false;
        })


         // 筛选状态
        //  form.on("select(select)", function(e){
        //     table.reload('productListTable', {
        //         page: {
        //             start: 1 //重新从第 1 页开始
        //         }
        //         ,where: {
        //             recommend: e.value
        //         }
        //     });
            
        //     setTimeout(function() {
        //         $("#status .layui-select-title input").attr("placeholder", e.value == '0' ? "待审核" : (e.value == "1" ? "已通过" : (e.value == '2' ? "已逐回" : "全部" )))
        //     }, 300)
            
        // })


        // 编辑和删除
        table.on("tool(productListTable)", function(data) {
            console.log(data);
            switch(data.event) {
                case "edit":
                    window.location.href = "../addProduct/addProduct.html?spuId=" + data.data.id;
                    break;
            }
        })
    });
</script>

</html>